#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce :
@File      : views.py
@Time      : 2023/9/28 15:18
@Author    : toby
"""

from django.http import JsonResponse
from django.shortcuts import render
from rest_framework_simplejwt.views import TokenObtainPairView
from .myserializer import MyTokenObtainPairSerializer

import json

def login_auth(request):
    if request.method == "POST":
        body = request.body
        json_result = json.loads(body)
        username = json_result['username']
        password = json_result['password']
        print(username, password)
        print(f"登陆username:{username}")
        print(f"密码password:{password}")

        if username.strip() == "root" and (password.strip() == "12345678hjw123"):
            res_data = {
            "code": 0,
            "username": username,
            "password": password,
                "message":" 登陆成功！"

            }
            return JsonResponse(res_data)
        else:
            res_data = {
            "code": -200,
            "message": "账户密码有误，请输入正确的号码!"
            }
            return JsonResponse(res_data)

    else:
        res_data = {
        "code": 405,
        "message": "客户端请求中的方法被禁止!"
        }
        return JsonResponse(res_data)




# token 验证  自己定义的序列化
class MyTokenObtainPairView(TokenObtainPairView):
    serializer_class = MyTokenObtainPairSerializer


def bad_request(request, exception,template_name='./templates/errors/page_400.html'):
    return render(request,template_name)

def permission_denied(request, exception,template_name='./templates/errors/page_403.html'):
    return render(request,template_name)

def page_not_found(request,exception, template_name='./templates/errors/page_404.html'):
    return render(request,template_name)

def server_error(request,template_name='./templates/errors/page_500.html'):
    return render(request,template_name)


